import { Component, OnInit, OnDestroy } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { NzMessageService } from 'ng-zorro-antd'

import { INewPageColumn } from '@shared/interfaces/common'
import { TradeChargeNewSystemComponent } from './system/system.component'
import { TradeChargeNewOrderComponent } from './order/order.component'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { ChargeStoreService } from './charge-base.service'
import { TradeService } from '../../trade-abstract.service'
import { PageService } from '@shared/services/page.service'
import { I18NService } from '@core'

@Component({
  selector: 'app-trade-charge-new',
  templateUrl: './new.component.html',
  styles: [],
})
export class TradeChargeNewComponent implements OnInit, OnDestroy {
  pages: INewPageColumn[] = []

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private msgSrv: NzMessageService,
    private store: ChargeStoreService,
    private tradeService: TradeService,
    private pageService: PageService,
    private i18n: I18NService,
  ) {}

  ngOnInit() {
    this.initColumns()
  }

  ngOnDestroy(): void {
    this.store.reset()
  }

  initColumns() {
    this.pages = [
      {
        component: new ComWindow(TradeChargeNewSystemComponent, { title: 'GENERAL.INFO.OTHER.SYSTEM_INFO' }),
      },
      {
        component: new ComWindow(TradeChargeNewOrderComponent, { title: 'TRADE.STATS.ORDER_INFO' }),
      },
    ]
  }
  submit() {
    const newStore = this.store.value
    this.tradeService.createOnlineChargeOrder(newStore).subscribe(resp => {
      /** 成功后不需要路由守卫 */
      this.pageService.triggerGuardAlive(false)
      this.msgSrv.success(this.trans('GENERAL.NOTIFICATION.CREATE_SUCCESS'))
      this.router.navigate(['../'], { relativeTo: this.route })
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
